package com.deep.spring.v4;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
@WebFilter
public class TestFilter implements Filter {

    @PostConstruct
    public void test() {
        log.error("TestFilter");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
        log.error("adfasd = {}", filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.debug("start to auth request validate...111");
        HttpServletRequest req = (HttpServletRequest) servletRequest;
//        req.setCharacterEncoding("utf8");
        String token = req.getParameter("token");

        if (token != null) {
            System.out.println("aaaaaaaaaaaaa");
            System.out.println(token);
            System.out.println("bbbbbbbbbbbbb");
            //    :TODO check token
            log.info("TestFilter success");
//            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            log.error("TestFilter failed");
        }

        filterChain.doFilter(servletRequest, servletResponse);

    }
}
